﻿using Furion;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Reflection;

namespace Huasky.Gm.Web.Core;

public static class Extensions
{
    /// <summary>
    /// 自定义日志输出格式
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection AddHuaskyLogger(this IServiceCollection services)
    {
        // 控制台输出日志格式
        services.AddConsoleFormatter(option =>
        {
            option.DateFormat = "yyyy-MM-dd HH:mm:ss";
            option.WithTraceId = true;
        });

        // info、warning、error日志输出格式
        services.AddFileLogging(options =>
        {
            options.FileNameRule = fileName => string.Format(fileName, DateTime.UtcNow, "info");
            options.WriteFilter = logMsg => logMsg.LogLevel == LogLevel.Information || logMsg.LogLevel == LogLevel.Warning || logMsg.LogLevel == LogLevel.Error;
        });
        // 错误日志输出
        services.AddFileLogging(options =>
        {
            options.FileNameRule = fileName => string.Format(fileName, DateTime.UtcNow, "err");
            options.WriteFilter = logMsg => logMsg.LogLevel == LogLevel.Error;
        });

        return services;
    }



}
